package menu_api

import (
	"benkendSystem/database"
	"benkendSystem/models"
	"github.com/gin-gonic/gin"
	"net/http"
)

// GetMenuFromRole 根据角色获取菜单
func GetMenuFromRole(c *gin.Context) {
	in := new(GetMenuFromRoleRequest)
	err := c.ShouldBindJSON(in)
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 500,
			"msg":  "参数错误",
		})
		return
	}
	var roleId int64
	err = database.DB.Model(new(models.Role)).Select("id").Where("name = ?", in.Name).First(&roleId).Error
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 500,
			"msg":  "操作失败",
		})
		return
	}
	var data []string
	err = database.DB.Model(new(models.Menu)).Select("menu.name").Joins("left join role_menu rm on menu.id = rm.menu_id").Where("rm.role_id = ?", roleId).Find(&data).Error
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 500,
			"msg":  "操作失败",
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code":   200,
		"msg":    "操作成功",
		"result": data,
	})

}
